
local require  = require;
local ngx      = ngx;
local debug    = debug;
local string   = string;
local tonumber = tonumber;
local setmetatable = setmetatable;
local pairs = pairs
local type  = type

local table = table
local table_concat =table.concat

----数据变量----
local uu         = require("modules.commhm.utils");
local log        = require("modules.commhm.log");
local ns_time    = require("modules.commhm.time");
local ns_const_vars  = require("modules.commhm.const_vars");


-- lua配置文件类
-- _VERSION = '0.12.1'
local _M = {}
local mt = { __index = _M }


function _M.new(self, path_root_, file_name_, env_ )
	--log.debug( "call CLuaConfig:new, file_name_=" .. file_name_ .. ", env=" .. env_ );
	local ins_ = {
		path_root = path_root_,
		file_name = file_name_,   ---文件名  ma_config  version servers_config
		env       = env_,         ---环境

		--file_path = "",      --文件纯目录
		--self.path_file_name, --完整文件路径
		--content   = "",      --文字内容
		--fn_md5    = "",      --文件的md5值
	};
	local ret_ = setmetatable(ins_, mt);
	return ret_
end



---获得完整路径
function _M.getFullPath(self)
	if  not self.path_file_name then
		local debug_ = "";
		-- 0 10 正式环境使用的是 xxx.lua
		-- 1    开发服使用的是   xxx_debug.lua
		-- 2 12 先遣服使用的是   xxx_test2.lua
		if  self.env == 0 or self.env == 10 then
			--正式环境
		elseif  self.env == 1 or self.env == 11 then
			--测试
			debug_ = "_debug";
		elseif  self.env == 2 or self.env == 12 then
			--先遣
			debug_ = "_test2";
		else
			--未知环境
			log.error( "unknown CLuaConfig env" .. uu.to_str(self.env) )
		end

		if  self.env >= 10 then
			---海外
			self.file_path      = self.path_root .. 'foreign/ma/'
			self.path_file_name = self.file_path .. self.file_name .. debug_ .. ".lua"
		else
			---国内
			self.file_path      = self.path_root .. 'ma/'
			self.path_file_name = self.file_path .. self.file_name .. debug_ .. ".lua"
		end
	end

	return  self.path_file_name
end



---获得文件的格式，返回 json lua ignore 格式
function _M.getCheckFileType(self)
	if  ns_const_vars.all_lua_configs.check_file_type and
		ns_const_vars.all_lua_configs.check_file_type[ self.file_name ]	then
		return ns_const_vars.all_lua_configs.check_file_type[ self.file_name ]
	end
	return "lua"
end


---获得内容的base64编码 ( 需要避免网络传输乱码 )
function _M.getContentBase64(self)
	self:getFullPath()
	if  self.path_file_name then
		local ret_ = uu.read_file_content_safe( self.path_file_name )
		if  ret_ and #ret_ > 0 then
			return  ngx.encode_base64( ret_ )
		end
	end
end


---保存base64编码 ( 需要避免网络传输乱码 )
function _M.setContentBase64(self, content_base64_, md5_ )
	self:getFullPath()
	if  self.path_file_name then
		local file_content_ = ngx.decode_base64(content_base64_)

		if  file_content_ then
			log.day_list( "center_server", "decode_base64_ok",   self.file_name, #content_base64_, #file_content_ )
		else
			log.day_list( "center_server", "decode_base64_fail", self.file_name, #content_base64_ )
		end

		local md5_now_ = ngx.md5( file_content_ )
		if  md5_ then
			if  md5_ == md5_now_ then
				uu.create_dir_if( self.file_path )
				uu.chmod( self.file_path, 777 )
				uu.write_file_content( self.path_file_name, file_content_ )
				uu.chmod( self.path_file_name, 777 )
				self.content = file_content_
				self.fn_md5  = md5_
				log.day_list( "center_server", "md5_compare_ok",   self.file_name, md5_ )
			else
				log.day_list( "center_server", "md5_compare_fail", self.file_name, md5_, md5_now_ )
				return 1
			end
		else
			uu.write_file_content( self.path_file_name, file_content_ )
			uu.chmod( self.path_file_name, 777 )
		end
		return 0
	end
end



---从旧文件中尝试加载，加载后会立即过期
function _M.loadFromOldFile(self)
	self:getFullPath()

	local file_content_type_ = 'unknown'

	if  self.path_file_name then
		local content_ = uu.read_file_content_safe( self.path_file_name )
		log.day_list( "center_server", "try_load_old", self.path_file_name, #content_ )
		if  content_ and  #content_ > 0 then
			local t_
			file_content_type_ = self:getCheckFileType()
			if  file_content_type_ == 'lua' then
				t_ = table.loadstring( content_ )  --lua
			elseif file_content_type_ == 'json' then
				t_ = uu.json2table( content_ )   --json
			elseif file_content_type_ == 'ignore' then
				t_ = {}  -- 忽略检查
			else
				-- 未知格式
			end

			if  t_ then
				self.content = content_
				self.fn_md5  = ngx.md5( self.content )
				log.day_list( "center_server", "load_old_ok", file_content_type_, self.path_file_name, self.fn_md5 )
				return true
			end
		end
	end

	log.day_list( "center_server", "load_old_fail", file_content_type_, self.path_file_name, self.fn_md5 )
	return false
end




return _M;
